#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "market.h"

int show_supplier(struct n_suppliers* suppliers);

int add_supplier(struct n_suppliers** suppliers)
{
	int i;
	int c;
	struct n_suppliers* new;
	struct n_suppliers* aux;
	new = (struct n_suppliers*) malloc(sizeof(struct n_suppliers));
	memset (new, 0, sizeof(struct n_suppliers));
	int n;
	printf ("Introduzca nombre proveedor: ");
	i = 0;
	while ((c = getchar()) != '\n' && i < (SIZE_NAME - 1)){
		new->name[i] = c;
		i++;
	}
	new->name[i] = '\0';
	
	printf ("Introduzca el CIF del preveedor: ");
	i = 0;
	while ((c = getchar()) != '\n' && i < (SIZE_CIF - 1)){
		new->cif[i] = c;
		i++;
	}
	new->cif[i] = '\0';
	new->next = NULL;
	if (*suppliers == NULL){
		*suppliers = new;
	} 
	else {
		aux = *suppliers;
		while (aux->next != NULL){
			aux = aux->next;
		}
		aux->next = new;
	}
	n = count_list_supplier(*suppliers);
	printf ("Proveedor añadido.\n");
	printf ("Usted tiene %d preveedores en tu base de datos.\n",n); 
	return 0;
}

int del_supplier(struct n_suppliers** suppliers)
{
	struct n_suppliers* aux;
	struct n_suppliers* del;
	char cif[SIZE_CIF];
	char c;
	int i;
	
	aux = *suppliers;

	if (*suppliers == NULL){
		printf ("No hay proveedores.\n");
		return 1;
	}
	else {
		printf ("Estos son los proveedotes que usted tiene.\n");
		show_supplier(&(**suppliers));
		printf ("Introduzca el CIF del proveedor que usted quiere borrar.\n");
		i = 0;
		while ((c = getchar()) != '\n' && i < (SIZE_CIF - 1)){
			cif[i] = c;
			i++;
		}
		cif[i] = '\0';
		if (strcmp(cif, aux->cif) == 0){
			del = *suppliers;
			*suppliers = aux->next;
			free(del);
		}
		else {
			printf ("Estos son los proveedotes que usted tiene.\n");
			show_supplier(&(**suppliers));
			printf ("Introduzca el CIF del proveedor que usted quiere borrar.\n");
			i = 0;
			while ((c = getchar()) != '\n' && i < (SIZE_CIF - 1)){
				cif[i] = c;
				i++;
			}
			cif[i] = '\0';
			while (strcmp(cif, aux->next->cif) != 0){
				if (aux->next == NULL){
					printf ("Proveedor no encontrado.\n");
					return 2;
				}
				aux = aux->next;
			}
			del = aux->next;
			aux->next = aux->next->next;
			free(del);
		}
	}
	return 0;
}

int edit_supplier(struct n_suppliers** suppliers)
{
	struct n_suppliers* aux;
	struct n_suppliers* edit;
	char cif[SIZE_CIF];
	int i, n, c;

	if (*suppliers == NULL){
		printf ("No hay proveedores.\n");
		return 1;
	}
	else {
		aux = *suppliers;
		printf ("Estos son los proveedotes que usted tiene.\n");
		show_supplier(&(**suppliers));
		printf ("Introduzca el CIF del proveedor que usted quiere editar.\n");
		i = 0;
		while ((c = getchar()) != '\n' && i < (SIZE_CIF - 1)){
			cif[i] = c;
			i++;
		}
		cif[i] = '\0';
		while (strcmp(cif, aux->cif) != 0){
			if (aux->next == NULL){
				printf ("Proveedor no encontrado.\n");
				return 2;
			}
			aux = aux->next;
		}
		edit = aux;
		printf ("Introduzca nombre proveedor:\n");
		i = 0;
		while ((c = getchar()) != '\n' && i < (SIZE_NAME - 1)){
			edit->name[i] = c;
			i++;
		}
		edit->name[i] = '\0';
	
		printf ("Introduzca el CIF del preveedor:\n");
		i = 0;
		while ((c = getchar()) != '\n' && i < (SIZE_CIF - 1)){
			edit->cif[i] = c;
			i++;
		}
		edit->cif[i] = '\0';
		n = count_list_supplier(*suppliers);
		printf ("Proveedor editado.\n");
		printf ("Usted tiene %d preveedores en tu base de datos.\n",n); 
	}
	return 0;
}

int show_supplier(struct n_suppliers* suppliers)
{
	struct n_suppliers* aux;
	aux = suppliers;
	int n;

	if (suppliers == NULL){
		printf ("No hay proveedores.\n");
		return 1;
	}
	else {
		while (aux != NULL){
			printf ("%s", aux->name);
			printf ("\t%s\n", aux->cif);
			aux = aux->next;
		}
	}
	n = count_list_supplier(suppliers);
	printf ("\nUsted tiene %d proveedores.\n", n);
	return 0;
}

int init_file_sup(FILE* file_sup, struct n_suppliers** supplier)
{
	struct n_suppliers* aux;
	struct n_suppliers tmp;
	int n, i, out = FALSE;

	aux = *supplier;

	if (feof(file_sup) != 0){
		printf ("No hay nada en el archivo.\n");
		return 0;
	}
	else {
		i = 0;
		do {
			n = fread(&tmp, sizeof(struct n_suppliers), 1, file_sup);
			tmp.next = NULL;
			if (n <= 0){
				out = TRUE;	
			}
			else {
				if (*supplier == NULL){
					*supplier = (struct n_suppliers*) malloc(sizeof(struct n_suppliers));
					memset (*supplier, 0, sizeof(struct n_suppliers));
					aux = *supplier;
					*aux = tmp;
					aux->next = NULL;
					i++;
				} else {
					while (aux->next != NULL){
						aux = aux->next;
					}
					aux->next = (struct n_suppliers*) malloc(sizeof(struct n_suppliers));
					memset (aux->next, 0, sizeof(struct n_suppliers));
					aux = aux->next;
					*aux = tmp;
					aux->next = NULL;
					i++;
				}
			}
		} while (out == FALSE);

//		aux->next = NULL;
		fclose(file_sup);
		printf ("You have loaded %d proveedores.\n", i);
		return 0;
	}
}

int record_data(struct n_suppliers** supplier)
{
	FILE* file_sup;
	struct n_suppliers* aux;
	struct n_suppliers* del;
	int n, i, sum;

	if (*supplier == NULL){
		printf ("No hay nada para escribir en la base de datos.\n");
		return 1;
	}
	else {
		if ((file_sup = fopen(F_SUP, "wb")) == NULL){
			printf ("Error al abrir el archivo  para escritura.\n");
			return 2;
		}
		aux = *supplier;

		i = count_list_supplier(*supplier);
		sum = 0;
		while (aux != NULL){
			n = fwrite(aux, sizeof(struct n_suppliers), 1, file_sup);
			sum = sum + n;
			del = aux;
			aux = aux->next;	
			free(del);	
		}
		if (sum != i){
			printf ("Error al volcar structs en el archivo.\n");
			printf ("sum != i\n");
			return 2;
		}
	}
	printf ("He volcado %d structs en el archivo.\n", sum);
	fclose (file_sup);
	return 0;
}

int man_supplier(void)
{
	FILE* file_sup; 
	struct n_suppliers* suppliers;
	suppliers = NULL;
	int option;
	char r;
	int firstrun, out;
	firstrun = FALSE;
	out = FALSE;

	if ((file_sup = fopen(F_SUP, "r+b")) == NULL){
		firstrun = TRUE;
		printf ("FIRST RUN...\n");
	} else 	if (firstrun == FALSE){
		printf ("NOT FIRST RUN.\n");
		init_file_sup (file_sup, &suppliers);
	}
	do {
		printf ("(1) para añadir proveedor\n");
		printf ("(2) para editar datos de proveedor\n");
		printf ("(3) para borrar proveedor\n");
		printf ("(4) para enseñar proveedor\n");
		printf ("(0) para volver al menu principal\n");
		scanf ("%d", &option);
		scanf ("%c", &r);
		switch (option){
			case 1:
				add_supplier(&suppliers);
				break;
			case 2:
				edit_supplier(&suppliers);
				break;
			case 3:
				del_supplier(&suppliers);
				break;
			case 4:
				show_supplier(suppliers);
				break;
			case 0:
				record_data(&suppliers);
				out = TRUE;
				return 0;
				break;
			default:
				printf ("Opcion no encontrada\n");
				break;
		}
	} while ( out == FALSE);
	return 0;
}
